using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using JetBrains.Annotations;
using Dyn.News.Models;
using Dyn.News.Routing;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;
using Orchard.Core.Routable.Models;
using Orchard.Core.Routable.Services;
using Orchard.Data;
using Orchard.Services;
using Orchard;

namespace Dyn.News.Handlers {
    [UsedImplicitly]
    public class CategoryPartHandler : ContentHandler {
        private readonly IWorkContextAccessor _workContextAccessor;
        private readonly ICategoryPathConstraint _categoryPathConstraint;
        private readonly IHomePageProvider _routableHomePageProvider;

        public CategoryPartHandler(IRepository<NewsCategoryPartRecord> repository, IWorkContextAccessor workContextAccessor, IEnumerable<IHomePageProvider> homePageProviders, ICategoryPathConstraint categoryPathConstraint) {
            _workContextAccessor = workContextAccessor;
            _categoryPathConstraint = categoryPathConstraint;
            _routableHomePageProvider = homePageProviders.SingleOrDefault(p => p.GetProviderName() == RoutableHomePageProvider.Name);
            Filters.Add(StorageFilter.For(repository));

            Action<PublishContentContext, RoutePart> publishedHandler = (context, route) => {
                if (route.Is<NewsCategoryPart>()) {
                    if (route.ContentItem.Id != 0 && route.PromoteToHomePage)
                        _categoryPathConstraint.AddPath("");
                }
                else if (route.ContentItem.Id != 0 && route.PromoteToHomePage) {
                    _categoryPathConstraint.RemovePath("");
                }
            };

            OnPublished<RoutePart>(publishedHandler);
            OnUnpublished<RoutePart>(publishedHandler);

            OnGetDisplayShape<NewsCategoryPart>((context, Category) => {
                context.Shape.Description = Category.Description;
                context.Shape.PostCount = Category.PostCount;
            });
        }

        protected override void GetItemMetadata(GetContentItemMetadataContext context) {
            var Category = context.ContentItem.As<NewsCategoryPart>();

            if (Category == null)
                return;

            var categoryPath = Category.Id == _routableHomePageProvider.GetHomePageId(_workContextAccessor.GetContext().CurrentSite.HomePage)
                ? ""
                : Category.As<RoutePart>().Path;

            context.Metadata.DisplayRouteValues = new RouteValueDictionary {
                {"Area", "Dyn.News"},
                {"Controller", "Category"},
                {"Action", "Item"},
                {"categoryPath", categoryPath}
            };
            context.Metadata.CreateRouteValues = new RouteValueDictionary {
                {"Area", "Dyn.News"},
                {"Controller", "CategoryAdmin"},
                {"Action", "Create"}
            };
            context.Metadata.EditorRouteValues = new RouteValueDictionary {
                {"Area", "Dyn.News"},
                {"Controller", "CategoryAdmin"},
                {"Action", "Edit"},
                {"categoryID", context.ContentItem.Id}
            };
            context.Metadata.RemoveRouteValues = new RouteValueDictionary {
                {"Area", "Dyn.News"},
                {"Controller", "CategoryAdmin"},
                {"Action", "Remove"},
                {"categoryID", context.ContentItem.Id}
            };
            context.Metadata.AdminRouteValues = new RouteValueDictionary {
                {"Area", "Dyn.News"},
                {"Controller", "CategoryAdmin"},
                {"Action", "Item"},
                {"categoryID", context.ContentItem.Id}
            };
        }
    }
}